package io.renren.modules.back.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.back.dao.GoodsSpecificationDao;
import io.renren.modules.back.dao.ProductDao;
import io.renren.modules.back.entity.GoodsSpecificationEntity;
import io.renren.modules.back.entity.ProductEntity;
import io.renren.modules.back.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.renren.common.utils.StringUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 产品service接口实现类
 *
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019年4月13日 上午15:28:55
 */
@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {
    @Autowired
    private GoodsSpecificationDao goodsSpecificationDao;

    @Override
    public List<ProductEntity> queryList(Map<String, Object> map) {
        List<ProductEntity> list = baseMapper.queryList(map);

        List<ProductEntity> result = new ArrayList<>();
        //翻译产品规格
        if (null != list && list.size() > 0) {
            for (ProductEntity item : list) {
                String specificationIds = item.getGoodsSpecificationIds();
                String specificationValue = "";
                if (!StringUtil.isNullOrEmpty(specificationIds)) {
                    String[] arr = specificationIds.split("_");
                    List<GoodsSpecificationEntity> GoodsSpecificationList = goodsSpecificationDao.selectBatchIds(Arrays.asList(arr));
                    for (GoodsSpecificationEntity goodsSpecification : GoodsSpecificationList) {
                        specificationValue += goodsSpecification.getValue() + "；";
                    }
                }
                item.setSpecificationValue(specificationValue);
                result.add(item);
            }
        }
        return result;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return baseMapper.queryTotal(map);
    }
}
